import {
    msg,
    close,
    getQueryStringFromHash
} from '../../common/utils.js';
import Vue from 'vue';
import {
    serverPath,
    json
} from '../../common/config.js';
import ClassService from '../services/classService.js';
import SchoolService from '../services/schoolService.js';

window.Module = function () {
    let classService = new ClassService();
    let schoolService = new SchoolService();
    return new Vue({
        el: '#detailPageController',
        data: {
            entity: {

            },
            schools:[]
        },
        methods: {
            init: async function () {
                await this.loadSchools();
                let id = getQueryStringFromHash('id');
                if (!id)
                    close();
                let res = await classService.getClassesById(id);
                if (json == res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.entity = response.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            },
            close: function () {
                close();
            },
            loadSchools: async function () {
                let res = await schoolService.getSchools(1,100);
                if (!res.type) {
                    return;
                }
                else if (json === res.type) {
                    var response = res.body;
                    if ('Success' === response.result) {
                        this.schools = response.data.data;
                    }
                    else {
                        await msg(response.message);
                    }
                }
            }
        }
    });
}